using System;
using System.Threading;

namespace EpisodeWizard.Core
{
	/// <summary>
	/// Summary description for ThreadedAction.
	/// </summary>
	public abstract class ThreadedAction
	{
		public delegate void StartHandler();
		public delegate void ProgressHandler(int Percent, string Text1, string Text2);
		public delegate void EndHandler();

		public event StartHandler Start;
		public event ProgressHandler Progress;
		public event EndHandler End;
		public bool CancelRequest;

	
		/// <summary>
		/// This is the main function of the thread
		/// Must be overridden
		/// </summary>
		public void StartAction()
		{
			Thread ActionThread = new Thread(new ThreadStart(this.Action));
			ActionThread.Start();
		}

		/// <summary>
		/// This is the main function of the thread
		/// Must be overridden
		/// </summary>
		public abstract void Action();

		/// <summary>
		/// Constructor
		/// </summary>
		public ThreadedAction()
		{
			CancelRequest = false;
		}
	
	
		public void OnStart()
		{
			if (Start != null)
			{
				Start();
			}
		}
	
		public void OnEnd()
		{
			if (End != null)
			{
				End();
			}
		}

		public void OnProgress(int Percent, string Text1, string Text2)
		{
			if (Progress != null)
			{
				Progress(Percent, Text1, Text2);
			}
		}
	
		/// <summary>
		/// Called to cancel the current action
		/// </summary>
		public void OnCancel()
		{
			CancelRequest = true;
		}
	}
}
